package SchemaVo::ModelVo;

use strict;
use warnings;
use utf8;

use lib './src';

use SchemaVo::ModelPropertyVo;

# 从属性参数创建
sub New {
    my ($class, %param) = @_;

    my $self = {
        # 模型 Id
        ModelId => undef,
        # 唯一标识符 Id
        IdentityId => undef,
        # 唯一标识名称
        IdentityName => undef,
        # 是否是聚合根
        IsAggregate => undef,
        # 模型名称
        Name => undef,
        # 模型说明
        Comment => undef,
        # 属性列表
        PropertyVos => undef,
    };

    foreach my $key (keys %param){
        $self->{$key} = $param{$key};
    }

    bless $self, $class;

    return $self;
}

# 从模型实体创建
sub FromModel {
    my ($class, $model) = @_;

    my @propertyVos = map { SchemaVo::ModelPropertyVo->FromProperty($_) } $model->ModelProperties->all;

    my $self = {
        # 模型 Id
        ModelId => $model->Id,
        # 唯一标识符 Id
        IdentityId => $model->IdentityId,
        # 唯一标识名称
        IdentityName => $model->Identity->Name,
        # 是否是聚合根
        IsAggregate => $model->IsAggregate,
        # 模型名称
        Name => $model->Name,
        # 模型说明
        Comment => $model->Comment,
        # 属性列表
        PropertyVos => \@propertyVos,
    };

    bless $self, $class;

    return $self;
}

# 返回普通的 Hash 表
sub ToPlainHash{
    my ($self) = @_;

    return {
        # 模型 Id
        ModelId => $self->{ModelId},
        # 唯一标识符 Id
        IdentityId => $self->{IdentityId},
        # 唯一标识名称
        IdentityName => $self->{IdentityName},
        # 是否是聚合根
        IsAggregate => $self->{IsAggregate},
        # 模型名称
        Name => $self->{Name},
        # 模型说明
        Comment => $self->{Comment},
        # 属性列表
        PropertyVos => [
            map { $_->ToPlainHash } @{ $self->{PropertyVos} }
        ],
    }
}

1;
